package cn.phecda.boot.data.jpa.support;

import cn.phecda.boot.common.lang.util.JacksonUtils;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Collections;
import java.util.Map;

/**
 * <p> Map转换与String转换 </p>
 *
 * @author : wanpinwei
 */
@Converter
public class HashMapConverter implements AttributeConverter<Map<String, Object>, String> {

    @Override
    public String convertToDatabaseColumn(Map<String, Object> object) {
        if (ObjectUtils.isEmpty(object)) {
            return null;
        }
        return JacksonUtils.toJson(object);
    }

    @Override
    public Map<String, Object> convertToEntityAttribute(String json) {
        if (ObjectUtils.isEmpty(json)) {
            return Collections.emptyMap();
        }
        return JacksonUtils.toMap(json, String.class, Object.class);
    }
}
